<?php
defined('_JEXEC') or die();

class WrdControllerJob extends JControllerLegacy 
{
        protected  $option;
        protected  $controller;
		function __construct( $default = array()){
    		$this->option = 'com_wrd';
    		$this->controller = 'job';
			parent::__construct( $default );
    	}

      	function display()
 		{
			switch($this->getTask()){
				case 'add':{
						JRequest::setVar( 'hidemainmenu', 1 );
						JRequest::setVar( 'layout', 'form'  );
						JRequest::setVar( 'view'  , 'job');
					} 
					break;
	            default:
					JRequest::setVar( 'view'  , 'job');
	            	break;
			}
			parent::display();
        }
        
      //save profile info
    function save(){
    	  
       $post=JRequest::get('post');
       $post['created_date'] = date("Y-m-d H:i:s");
       $params = JComponentHelper::getParams('com_wrd');
       $model=$this->getModel('job');
	   //get date current
       $id_category=$model->store($post,'job');
       if($id_category!=0){
			$this->setRedirect('index.php?option=com_wrd&controller=job','Saved');
			return true;                
       }
       else{
            $this->setRedirect('index.php?option=com_wrd&controller=job','Error Saved');
            return false;
       }
    }    
   
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('job');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=job' );
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('job');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=job' );
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('job');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option=com_wrd&controller=job' );
	}
	
	public function saveorder(){
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Get the arrays from the Request
		$order	= JRequest::getVar('order',	null, 'post', 'array');
		$originalOrder = explode(',', JRequest::getString('original_order_values'));

		// Make sure something has changed
		if (!($order === $originalOrder)) {
			// Check for request forgeries.
			JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
	
			// Get the input
			$pks	= JRequest::getVar('cid',	null,	'post',	'array');
			$order	= JRequest::getVar('order',	null,	'post',	'array');
	
			// Sanitize the input
			JArrayHelper::toInteger($pks);
			JArrayHelper::toInteger($order);
	
			// Get the model
			$model = $this->getModel('job');
	
			// Save the ordering
			$return = $model->saveorder($pks, $order,'job');
	
			if ($return === false)
			{
				// Reorder failed
				$message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
				$this->setRedirect(JRoute::_('index.php?option=com_wrd&controller=job', false), $message, 'error');
				return false;
			} else{
				// Reorder succeeded.
				$this->setMessage(JText::_('JLIB_APPLICATION_SUCCESS_ORDERING_SAVED'));
				$this->setRedirect(JRoute::_('index.php?option=com_wrd&controller=job', false));
				return true;
			}
		} else {
			// Nothing to reorder
			$this->setRedirect(JRoute::_('index.php?option=com_wrd&controller=job', false));
			return true;
		}
	}
	
	public function orderdown(){
		$this->reorder($task = 'orderdown');
	}
	
	public function orderup(){
		$this->reorder($task = 'orderup');
	}
	
	
	public function reorder($task)
	{
		// Check for request forgeries.
		JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialise variables.
		$user	= JFactory::getUser();
		$ids	= JRequest::getVar('cid', null, 'post', 'array');
		$inc	= ($task == 'orderup') ? -1 : +1;

		$model = $this->getModel('job');
		$return = $model->reorder($ids, $inc,'job');
		if ($return === false) {
			// Reorder failed.
			$message = JText::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError());
			$this->setRedirect(JRoute::_('index.php?option='.$this->option.'&controller='.$this->controller, false), $message, 'error');
			return false;
		} else {
			// Reorder succeeded.
			$message = JText::_('JLIB_APPLICATION_SUCCESS_ITEM_REORDERED');
			$this->setRedirect(JRoute::_('index.php?option='.$this->option.'&controller='.$this->controller, false), $message);
			return true;
		}
	}
}